/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.topisv.api.response;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.Header;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.qdss.topisv.api.Result;

/**
 * XML结果集
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @since 0.1.0, Nov 17, 2009
 * @version $Id: XmlResultImpl.java 70 2012-09-08 16:29:34Z zhaofang123 $
 */
public class XmlResultImpl implements Result {
	private static final long serialVersionUID = 8498014774012078068L;
	
	/**
	 * 当获取数字类型的值时，如果为空则返回此值
	 */
	public static int DEFAULT_NUMBER_VALUE = -1;
	
	final private Map<String, Header> headerMap;
	final private int invokedTime;
	final private String content;
	private Element rootElement;
	
	/**
	 * @param headers
	 * @param invokedTime
	 * @param xmlContent
	 */
	public XmlResultImpl(Header[] headers, int invokedTime, String xmlContent) {
		this.headerMap = new HashMap<String, Header>();
		this.invokedTime = invokedTime;
		
		for (Header h : headers) {
			this.headerMap.put(h.getName(), h);
		}
		this.content = xmlContent;
	}
	
	@Override
	public Header getHeader(String headerName) {
		return headerMap.get(headerName);
	}
	
	@Override
	public String getHeaderValue(String headerName) {
		Header h = getHeader(headerName);
		if (h == null) {
			return null;
		}
		return h.getValue();
	}

	@Override
	public Element getNode(String xpath) {
		return (Element) rootElement().selectSingleNode(xpath);
	}
	
	@Override
	public boolean hasNode(String xpath) {
		return rootElement().selectSingleNode(xpath) != null;
	}
	
	@Override
	public String getStringValue(String xpath) {
		Element element = getNode(xpath);
		if (element == null) {
			return null;
		}
		return element.getText();
	}
	
	@Override
	public int getIntValue(String xpath) {
		Element element = getNode(xpath);
		if (element == null) {
			return DEFAULT_NUMBER_VALUE;
		}
		return NumberUtils.toInt(element.getText(), DEFAULT_NUMBER_VALUE);
	}

	@Override
	public long getLongValue(String xpath) {
		Element element = getNode(xpath);
		if (element == null) {
			return DEFAULT_NUMBER_VALUE;
		}
		return NumberUtils.toLong(element.getText(), DEFAULT_NUMBER_VALUE);
	}
	
	@Override
	public double getDoubleValue(String xpath) {
		Element element = getNode(xpath);
		if (element == null) {
			return DEFAULT_NUMBER_VALUE;
		}
		return NumberUtils.toDouble(element.getText(), DEFAULT_NUMBER_VALUE);
	}
	
	@Override
	public long getInvokedTime() {
		return invokedTime;
	}
	
	@Override
	public Element rootElement() {
		if (rootElement != null) {
			return rootElement;
		}
		
		if (StringUtils.isBlank(content)) {
			throw new ErrorRspException("Blank result!");
		}
		
		try {
			rootElement = DocumentHelper.parseText(content).getRootElement();
		} catch (DocumentException e) {
			throw new ErrorRspException("Bad xml content: " + content);
		}
		return rootElement;
	}
	
	/**
	 * 获取原始内容
	 * 
	 * @return
	 */
	public String getOrigContent() {
		return content;
	}
}
